package com.dhl.utils;

import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

/**
 * 常用工具类
 * @author hai
 *
 */
public class Utils {
	
	

	/**
	 * 验证参数是否不为空,非空=true
	 * @param object
	 * @return
	 */
	public static boolean isNotNull(Object object) {
		return object != null && object != "" && object.toString().length() > 0;
	}
	
	
	/**
	 * 写入boolean值
	 * @param b
	 */
	public static void print(boolean b){
		PrintWriter pw = null;
		try {
			ServletActionContext.getResponse().setHeader("content-type", "text/html;charset=UTF-8");
			pw = ServletActionContext.getResponse().getWriter();
			pw.print(b);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(pw != null){
				pw.close();
			}
		}
	}
	
	/**
	 * 写入String
	 * @param b
	 */
	public static void print(String s){
		PrintWriter pw = null;
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setHeader("content-type", "text/html;charset=UTF-8");
			pw = response.getWriter();
			pw.print(s);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(pw != null){
				pw.close();
			}
		}
	}
	
	/**
	 * 去掉第一个字符
	 * @param s
	 * @return
	 */
	public static String subOne(String s){
		if(Utils.isNotNull(s)){
			return s.substring(1);
		}
		return "";
	}
	
	/**
	 * 判断date1和date2是否在同一周
	 * @param date1
	 * @param date2
	 * @return true表示是一周，false不是一周
	 */
	 public static boolean isSameDate(Date date1,Date date2)
	 {
	  Calendar cal1 = Calendar.getInstance();
	  Calendar cal2 = Calendar.getInstance();
	  cal1.setTime(date1);
	  cal2.setTime(date2);
	  int subYear = cal1.get(Calendar.YEAR)-cal2.get(Calendar.YEAR);//两个年相减
	  if(subYear == 0)//说明是同一年
	  {
	   if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) //当前年第几周是否相同
	    return true;
	  }else if(subYear==1 && cal2.get(Calendar.MONTH)==11)//周是否相同，例子:cal1是"2005-1-1"，cal2是"2004-12-25"
	  {
	   if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
	    return true;
	  } else if(subYear==-1 && cal1.get(Calendar.MONTH)==11) //周是否相同，例子:cal1是"2004-12-31"，cal2是"2005-1-1"
	  {
	   if(cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
	    return true;
	  }
	  return false;
	 }
	
	public static void main(String[] args) {
		System.out.println(subOne("Ab"));
	}
	
}
